/*
 * Copyright 2012 yura.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.opu.pcm.services;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.opu.pc.domain.UserManagement;
import org.opu.pc.domain.domains.security.SecurityGroup;
import org.opu.pc.domain.domains.security.SecurityGroupHolder;
import org.opu.pc.domain.domains.security.User;
import org.opu.pcm.services.beans.UserBean;

/**
 *
 * @author yura
 */
public class UserServiceImpl implements UserService{
    
    private UserManagement userManagement;

    public UserServiceImpl(UserManagement userManagement) {
        this.userManagement = userManagement;
    }

    @Override
    public void ensureDataRegistered() {
        userManagement.ensureDataRegistered();
    }

    @Override
    public List<UserBean> list() {
        List<User> list = userManagement.list();
        List<UserBean> beans = new ArrayList<UserBean>();
        for (User user : list) {
            beans.add(toBean(user));
        }
        return beans;
    }

    @Override
    public void createUser(UserBean user) {
        userManagement.createUser(toDomain(user));
    }

    @Override
    public void updateUser(UserBean user) {
        userManagement.updateUser(toDomain(user));
    }

    @Override
    public void deleteUser(UserBean user) {
        userManagement.deleteUser(user.getId());
    }
    
    @Override
    public UserBean getGuestAccount(){
        return toBean(userManagement.getGuestAccount());
    }
    
    private UserBean toBean(User domain){
        UserBean bean = new UserBean(domain.getId(), domain.getName(), 
                domain.getPassword(), domain.getEmail());
        List<UserBean.SecurityGroup> list = new ArrayList<UserBean.SecurityGroup>();
        for (SecurityGroupHolder s : domain.getSecurityGroups()) {
            switch (s.getId()){
                case Operator:
                    list.add(UserBean.SecurityGroup.Operator);
                    break;
                case Professor:
                    list.add(UserBean.SecurityGroup.Professor);
                    break;
                case Secretary:
                    list.add(UserBean.SecurityGroup.Secretary);
                    break;
                default :
                    throw new RuntimeException();
            }
        }
        bean.setSecurityGroups(list);
        return bean;
    }
    
    private User toDomain(UserBean bean){
        User domain = new User(bean.getId(), bean.getName(), bean.getPassword(),
                bean.getEmail());
        Set<SecurityGroupHolder> set = new HashSet<SecurityGroupHolder>();
        for (UserBean.SecurityGroup s : bean.getSecurityGroups()) {
            switch (s){
                case Operator:
                    set.add(new SecurityGroupHolder(SecurityGroup.Operator));
                    break;
                case Professor:
                    set.add(new SecurityGroupHolder(SecurityGroup.Professor));
                    break;
                case Secretary:
                    set.add(new SecurityGroupHolder(SecurityGroup.Secretary));
                    break;
                default :
                    throw new RuntimeException();
            }
        }
        domain.setSecurityGroups(set);
        return domain;
    }
    
}
